package com.example.zluserservice.controller;

import com.example.zluserservice.client.ResourceFeignClient;
import com.example.zluserservice.common.base.ResponseResult;
import com.example.zluserservice.common.util.UserThreadLocal;
import com.example.zluserservice.dto.request.ResourceReqDTO;
import com.example.zluserservice.dto.response.MenuResDTO;
import com.example.zluserservice.dto.response.ResourceResDTO;
import com.example.zluserservice.dto.response.TreeResDTO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author zhenliu（孙凌岳）
 * @Description 用户服务 - 资源信息控制器（RPC调用）
 * @Date 2025/6/16 10:27
 * @Version 1.0
 */
@Slf4j
@Api(tags = "用户服务 - 资源信息控制器（RPC）")
@RestController
@RequestMapping("/user/resource")
public class ResourceController {
    @Resource
    private ResourceFeignClient resourceFeignClient;

    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    @ApiOperation(value = "根据资源ID删除菜单", notes = "通过资源ID删除指定菜单")
    public ResponseResult remove(@PathVariable("menuId") String menuId) {
        return ResponseResult.success(resourceFeignClient.deleteMenuById(menuId));
    }

    /**
     * @param resourceReqDTO 资源DTO对象
     * @return Boolean 是否修改成功
     *  修改资源
     */
    @PatchMapping
    @ApiOperation(value = "修改资源信息", notes = "修改资源的各项属性")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {
            "resourceDto.id",
            "resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"})
    public ResponseResult<Boolean> updateResource(@RequestBody ResourceReqDTO resourceReqDTO) {
        return ResponseResult.success(resourceFeignClient.updateResource(resourceReqDTO));
    }

    /**
     * @param resourceReqDTO 资源对象
     * @return
     *
     */
    @PostMapping("/tree")
    @ApiOperation(value = "查询资源树结构", notes = "获取资源的树形结构")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.label"})
    public ResponseResult<TreeResDTO> resourceTreeVo(@RequestBody ResourceReqDTO resourceReqDTO) {
        TreeResDTO treeVo = resourceFeignClient.resourceTreeResDTO(resourceReqDTO);
        return ResponseResult.success(treeVo);
    }

    /***
     *  多条件查询资源列表
     * @param resourceReqDTO 资源Vo对象
     * @return List<ResourceVo>
     */
    @PostMapping("/list")
    @ApiOperation(value = "多条件查询资源列表", notes = "根据条件查询资源列表")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {
            "resourceReqDTO.parentResourceNo",
            "resourceReqDTO.resourceType"
    })
    public ResponseResult<List<ResourceResDTO>> resourceList(@RequestBody ResourceReqDTO resourceReqDTO) {
        List<ResourceResDTO> resourceResDTOList = resourceFeignClient.findResourceList(resourceReqDTO);
        return ResponseResult.success(resourceResDTOList);
    }


    /**
     * 获取当前用户左侧菜单列表
     * @return
     */
    @GetMapping("/menus")
    @ApiOperation(value = "获取当前用户左侧菜单列表", notes = "获取当前登录用户的左侧菜单")
    public ResponseResult<List<MenuResDTO>> menus() {
        Long mgtUserId = UserThreadLocal.getMgtUserId();
        List<MenuResDTO> menus = resourceFeignClient.menus(mgtUserId);
        return ResponseResult.success(menus);
    }

    /**
     * 获取当前用户按钮列表
     * @return
     */
    @GetMapping("/buttons")
    @ApiOperation(value = "获取当前用户按钮列表", notes = "获取当前登录用户的按钮权限列表")
    public ResponseResult<List<MenuResDTO>> buttons(Long userId) {
        Long mgtUserId = UserThreadLocal.getMgtUserId();
        List<MenuResDTO> menus = resourceFeignClient.buttons(mgtUserId);
        return ResponseResult.success(menus);
    }
}
